package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.*;
import com.zero.mapper.FamilyMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.mapper.InvestMapper;
import com.zero.mapper.InvestTypeMapper;
import com.zero.util.DateUtils;
import com.zero.util.Result;
import com.zero.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class InvestService {

    @Autowired
    private AssetsDebtService assetsDebtService;

    @Autowired
    private InvestMapper investMapper;

    @Autowired
    private InvestTypeMapper investTypeMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Transactional
    public Result add(Invest invest, HttpServletRequest request){
        if (invest == null)
            return Result.fail("获取不到添加的信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");

        //获取用户的具体信息
        familyMember  = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());

        //更新家庭和个人资产流出
        familyMember.setDebt(familyMember.getDebt().add(invest.getMoney()));
        family.setDebt(family.getDebt().add(invest.getMoney()));
        familyMemberMapper.update(familyMember);
        familyMapper.update(family);

        invest.setMemberId(familyMember.getId());
        invest.setFamilyId(familyMember.getFamilyId());
        invest.setStatus(0);
        int row = investMapper.add(invest);
        if (row == 1)
            return Result.ok("添加投资信息成功");
        return Result.fail("添加投资信息失败");
    }

    @Transactional
    public Result delete(String ids){
        if (ids == null)
            return Result.fail("获取不到要删除的记录");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            row += investMapper.delete(Integer.parseInt(id));
        }
        if (row > 0)
            return Result.ok("成功删除"+ row +"条记录");
        return Result.fail("删除失败");
    }

    public Result update(Invest invest, HttpServletRequest request){
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");
        if (invest == null)
            return Result.fail("获取不到修改的信息");

        //获取家庭和个人信息，并更新其资产流出
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        Invest oldInvest = investMapper.selectById(invest.getId());
        familyMember.setDebt(familyMember.getDebt().subtract(oldInvest.getMoney()).add(invest.getMoney()));
        family.setDebt(family.getDebt().subtract(oldInvest.getMoney()).add(invest.getMoney()));
        familyMemberMapper.update(familyMember);
        familyMapper.update(family);

        int row = investMapper.update(invest);
        if (row == 1)
            return Result.ok("修改投资信息成功");
        return Result.fail("修改投资信息失败");
    }

    public Result select(Condition condition, HttpServletRequest request){
        if (condition == null)
            return Result.fail("获取不到具体的查询信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        //设置分页查询信息
        condition.setFamilyId(familyMember.getFamilyId());
        if (condition.getPage() != null){
            if (condition .getLimit() != null)
                PageHelper.startPage(condition.getPage(), condition.getLimit());
            else
                PageHelper.startPage(condition.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<Invest> invests = investMapper.selectByCondition(condition);
        for (Invest temp : invests) {
            temp.setFamilyMember(familyMemberMapper.selectById(temp.getMemberId()));
            if (temp.getMemberId() == familyMember.getId())
                temp.setFlag(0);
            else
                temp.setFlag(1);
            temp.setInvestType(investTypeMapper.selectById(temp.getTypeId()));

        }

        PageInfo pageInfo = new PageInfo(invests);
        return Result.ok(invests, pageInfo.getTotal());
    }

    /**
     * 回收投资
     * @param map
     * @param request
     * @return
     */
    @Transactional
    public Result retrieveInvest(Map map, HttpServletRequest request) {
        if(map == null || !map.containsKey("id") || !map.containsKey("money"))
            return Result.fail("获取不到投资的回收信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        //获取并更新个人资产流入
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        familyMember.setAssets(familyMember.getAssets().add(new BigDecimal(map.get("money") + "")));
        familyMemberMapper.update(familyMember);

        //修改家庭资产流入
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        BigDecimal assets = family.getAssets().add(new BigDecimal(map.get("money") + "")) ;
        family.setAssets(assets);
        familyMapper.update(family);

        //标志投资已回收
        Invest invest = new Invest();
        invest.setStatus(1);
        invest.setId((Integer)map.get("id"));
        invest.setEndDate(new Date());
        investMapper.update(invest);

        return Result.ok("已回收");
    }

    public List<Invest> getMemberTodayData(Integer memberId){
        if (memberId == null)
            return null;
        Invest investParam = new Invest();
        investParam.setMemberId(memberId);
        investParam.setStartDate(DateUtils.getToday());
        List<Invest> result = investMapper.select(investParam);
        return result;
    }

    public List<Invest> getFamilyTodayData(Integer familyId){
        if (familyId == null)
            return null;
        Invest investParam = new Invest();
        investParam.setFamilyId(familyId);
        investParam.setStartDate(DateUtils.getToday());
        List<Invest> result = investMapper.select(investParam);
        return result;
    }
}
